<!DOCTYPE html>
<head>
    <title>商品详情</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <script type="text/javascript" src="/js/jquery.min.js"></script>
    <link rel="stylesheet" type="text/css" href="/bootstrap/css/bootstrap.min.css"/><!-- bootstrap -->
    <script type="text/javascript" src="/bootstrap/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="/jquery-validation/jquery.validate.min.js"></script> <!-- jquery-validator -->
    <script type="text/javascript" src="/jquery-validation/localization/messages_zh.min.js"></script>
    <script type="text/javascript" src="/layer/layer.js"></script><!-- layer -->
    <script type="text/javascript" src="/js/md5.min.js"></script><!-- md5.js -->
    <script type="text/javascript" src="/js/common.js"></script><!-- common.js -->
    <script type="text/javascript" src="/js/socket.js"></script><!-- common.js -->
</head>
<body>
<div class="panel panel-default">
    <div class="panel-heading">秒杀商品详情</div>
    <div class="panel-body">
        <div id="userTip" style="display: none">
            <span> 您还没有登录，请<a href="/login.html">登陆</a>后再操作<br/></span>
        </div>
        <span>没有收货地址的提示。。。</span>
    </div>
    <table class="table">
        <tr>
            <td>商品名称</td>
            <td colspan="3" id="goodName"></td>
        </tr>
        <tr>
            <td>商品图片</td>
            <td colspan="3"><img id="goodImg" width="200" height="200"/></td>
        </tr>
        <tr>
            <td>秒杀开始时间</td>
            <td id="startDate"></td>
            <td id="seckillTip">
            </td>
            <td>
                <img id="verifyCodeImg" width="80" height="32" onclick="initVerifyCodeImg()" style="display: none">


                <input id="verifyCode" style="display: none">
                <button class="btn btn-primary btn-block" type="button" disabled id="buyButton" onclick="getPath()">
                    立即秒杀
                </button>
            </td>
        </tr>
        <tr>
            <td>商品原价</td>
            <td colspan="3" id="goodPrice"></td>
        </tr>
        <tr>
            <td>秒杀价</td>
            <td colspan="3" id="seckillPrice"></td>
        </tr>
        <tr>
            <td>库存数量</td>
            <td colspan="3" id="stockCount"></td>
        </tr>
    </table>
</div>
<script>
    createScoket("1111111");
    var seckillId = getQueryString("seckillId");
    console.log(seckillId);
    $.ajax({
        url: "http://localhost:9000/seckill/seckillGood/queryById?seckillId=" + seckillId,
        type: "get",
        //对于cookie处理能力
        xhrFields: {withCredentials: true},
        success: function (data) {
            //{code:200,msg:操作成功.data:vo}
            if (data.code == 200) {
                //{code:200,msg:操作成功.data:vo}
                renderData(data.data);
                //设置秒杀开始和结束时间
                renderDate(data.data.startDate, data.data.endDate);

            } else {
                //{code:5o201,msg:""}
                layer.msg("错误码: " + data.code + "  错误信息: " + data.msg);
            }
        }
    })


    $.ajax({
        url: "http://localhost:9000/member/getCurrent",
        type: "get",
        //对于cookie处理能力
        xhrFields: {withCredentials: true},
        success: function (data) {
            //{code:200,msg:操作成功.data:vo}
            if (data.code == 200) {
                showUserStatus(data.data);
            } else {
                //{code:5o201,msg:""}
                layer.msg("错误码: " + data.code + "  错误信息: " + data.msg);
            }
        }
    })

    function getPath() {
        //隐藏秒杀地址
        $.ajax({
            url: "http://localhost:9000/seckill/getPath",
            type: "post",
            data: {"uuid": uuid,"verifyCode":$("#verifyCode").val()},
            //对于cookie处理能力
            xhrFields: {withCredentials: true},
            success: function (data) {
                //{code:200,msg:操作成功.data:vo}
                if (data.code == 200) {
                    //生成UUID,data.data可以拿到path
                    doSeckill(data.data);
                } else {
                    //{code:5o201,msg:""}
                    layer.msg("错误码: " + data.code + "  错误信息: " + data.msg);
                }
            }
        })
    }


    //对于用户是否登录判断
    var u;
    var uuid;

    function showUserStatus(user) {
        if (!user) {
            $("#userTip").show();
            return;
        }
        u = user;
        uuid = getUuid(user.id);
        $("#verifyCodeImg").show();
        $("#verifyCode").show();
        initVerifyCodeImg();
    }
    function initVerifyCodeImg() {
    $("#verifyCodeImg").prop("src","http://localhost:9000/seckill/getVerifyCodeImg?uuid="+uuid+"&time="+new Date().getTime());
    }


    function doSeckill(path) {
        if (!u) {
            layer.msg("请先登录以后再进行操作");
            return
        }
        $.ajax({
            url: "http://localhost:9000/seckill/" + path + "/doSeckill",
            type: "post",
            data: {"seckillId": seckillId, "uuid": uuid},
            //对于cookie处理能力
            xhrFields: {withCredentials: true},
            success: function (data) {
                //{code:200,msg:操作成功.data:vo}
                if (data.code == 200) {
                    //返回一个订单编号
                    //Result  {code:200 ,msg:操作成功 ,data:orderNo}
                    // window.location.href="/order_detail.html?orderNo="+data.data;
                    layer.msg(data.data);
                    //和websocket建立联系
                    createScoket(uuid);

                } else {
                    //{code:5o201,msg:""}
                    layer.msg("错误码: " + data.code + "  错误信息: " + data.msg);
                }
            }
        })
    }


    var remainStartDate;//距离开始的时间
    var remainEndDate;//距离结束的时间
    function renderDate(start, end) {
        var startDate = new Date(start);
        var endDate = new Date(end);
        var now = new Date();
        //parseInt取整数方法
        remainStartDate = parseInt((startDate.getTime() - now.getTime()) / 1000);
        remainEndDate = parseInt((endDate.getTime() - now.getTime()) / 1000);
        //setInterval方法每间隔多长时间1000及间隔1秒钟执行 函数function inteval() {}
        setInterval(inteval, 1000);
    }

    function inteval() {
        remainStartDate--;
        remainEndDate--;
        if (remainStartDate > 0) {
            $("#seckillTip").html("距离秒杀开始还有: " + remainStartDate + "秒");
        } else if (remainEndDate > 0) {
            $("#seckillTip").html("距离结束还有: " + remainEndDate + "秒");
            $("#buyButton").prop("disabled", false);
        } else {
            $("#seckillTip").html("秒杀结束");
        }
    }


    function renderData(data) {
        $("#goodName").html(data.goodName);
        $("#goodImg").prop("src", data.goodImg);
        $("#startDate").html(data.startDate);
        $("#goodPrice").html(data.goodPrice);
        $("#seckillPrice").html(data.seckillPrice);
        $("#stockCount").html(data.stockCount);
    }


</script>
</body>
</html>